package com.study.services;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.jdao.IServiceDAO;
import com.study.model.mdao.LogforcustDAO;
import com.study.model.mdao.ServiceDAO;
import com.study.model.pojo.Colle;
import com.study.model.pojo.Complaint;
import com.study.model.pojo.Customer;
import com.study.model.pojo.Linkman;
import com.study.model.pojo.Service;
import com.study.model.pojo.User;
import com.study.model.pojo.WeixiuCart;
import com.study.model.vo.CartBox;
@org.springframework.stereotype.Service
@Transactional(rollbackOn = Exception.class)
public class ServiceSerivce {
	@Autowired
	ServiceDAO mapper;
	@Autowired
	IServiceDAO iservice;
	@Autowired
	CustomerService custService;
	@Autowired
	DictionaryService dservice;
	@Autowired
	LinkmanService lservice;
	@Autowired
	UserService uservice;
	@Autowired
	WeixiuService weixiuService;
	@Autowired
	LogforcustDAO logforcustDAO;
	
	/**
	 * 根据条件查询售后服务
	 * @param cusid 
	 * @param i
	 * @param j
	 * @param string
	 * @param string2
	 * @return
	 */
	public List<JSONObject> selectBySel(int pageNum, int pageSize, String input, String selects, String cusid) {
		String type = null;
		String mode = null;
		String state = null;
		PageHelper.startPage(pageNum, pageSize);
		if (selects.length() > 0 && selects.startsWith("40")) {
			type = selects;
		} else if (selects.length() > 0 && selects.startsWith("41")) {
			mode = selects;
		}else if (selects.length() > 0 && selects.startsWith("42")){
			state = selects;
		}
		return mapper.selectByWhere(type, mode,state, "%"+input+"%",cusid);
	}
	
	/**
	 * 保存一条服务
	 */
	public boolean addService(String name, String custid, String type, 
			String mode, Date date1, String spendtime,String linId,
			String state, String useid, String content, String back,String more) {
		boolean flag = false;
		double parseDouble = 0.0;
		if (spendtime != null && !"".equals(spendtime)) {
			 parseDouble = Double.parseDouble(spendtime);
		}
	
		Service service = new Service();
		Customer customer = custService.findById(Integer.parseInt(custid));
		Linkman alinkman = lservice.selectById(Integer.parseInt(linId));
		service.setDictionary1(dservice.findByDicId(Integer.parseInt(type)));
		
		service.setDictionary2(dservice.findByDicId(Integer.parseInt(mode)));
		
		service.setDictionary3(dservice.findByDicId(Integer.parseInt(state)));
		
		User auser = uservice.findById(Integer.parseInt(useid));
		service.setCustomer(customer);
		service.setLinkman(alinkman);
		service.setSerContent(content);
		service.setSerFeednack(back);
		service.setSerName(name);
		service.setSerRemark(more);
		service.setSerStart(date1);
		service.setSerSpendtime(parseDouble);
		service.setUser(auser);
		service.setIsShow(1);
		try {
			iservice.save(service);
			flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Integer id = Integer.parseInt(custid);
		Integer useId = Integer.parseInt(useid);
		
		//添加一条流水
		String logMessage = "客户服务&nbsp;&nbsp;"+name +"===="+customer.getCusName();

		logforcustDAO.insert(id, new Date(), useId, 228, logMessage, "");

		return flag;
	}
	
	/**
	 * 根据id查询一条记录
	 * @param parameter
	 * @return
	 */
	public JSONObject selectBycid(String parameter) {
		return mapper.selectById(parameter);
	}

	/**
	 * 修改服务
	 * @param service
	 * @param id 
	 * @return
	 */
	public boolean editorService(String name, String custid, String type, String mode, Date date1, String spendtime, String linId, String state, Integer useid, String content, String back, String more,
			String id) {
		
		double parseDouble = 0.0;
		if (spendtime != null) {
			 parseDouble = Double.parseDouble(spendtime);
		}
		
		Service service = iservice.findBySerId(Integer.parseInt(id));
		Customer customer = custService.findById(Integer.parseInt(custid));
		System.out.println(2);
		Linkman alinkman = lservice.findById(Integer.parseInt(linId));
		//System.out.println(1+linkman);
		service.setDictionary1(dservice.findByDicId(Integer.parseInt(type)));
		System.out.println(1+type);
		service.setDictionary2(dservice.findByDicId(Integer.parseInt(mode)));
		System.out.println(1+mode);
		service.setDictionary3(dservice.findByDicId(Integer.parseInt(state)));
		System.out.println(1+state);
		User auser = uservice.findById(useid);
		System.out.println(2);
		service.setCustomer(customer);
		service.setLinkman(alinkman);
		service.setSerContent(content);
		service.setSerFeednack(back);
		service.setSerName(name);
		service.setSerRemark(more);
		service.setSerStart(date1);
		
		service.setSerSpendtime(parseDouble);
		service.setUser(auser);
		return true;
	}
	/**
	 * 删除服务
	 * @param goo_id
	 */
	public void delete(int cid ) {
		Service service = iservice.findById(cid).get();
		//判断该条服务是否为电话服务，如果为，则只进行删除，其他只进行逻辑删除。
				if (service.getDictionary2().getDicId() != 411) {
					service.setIsShow(0);
				}else {
					iservice.delete(service);
				}
	}

	/**
	 * 根据服务方式分类
	 */
	public List<JSONObject> selectByMode(){
		return mapper.selectByMode();
	}

	public List<JSONObject> selectByCount() {
		
		return  mapper.selectByCount();
	}
	
	/**
	 * 根据custName查询可维修的订单及产品
	 * @param msg
	 * @return
	 */
	public List<WeixiuCart> queryByLike(String msg) {
		
		List<JSONObject> orders = null;
		List<WeixiuCart> listCart = new ArrayList<>();
		
		List<JSONObject> customer = custService.getfindCustName(msg);
		
		if (customer!= null && customer.size() > 0) {
			String custid = customer.get(0).getString("cus_id");
			orders = weixiuService.findOrderAndGoods(custid);
		}
		
		if (orders!= null && !orders.isEmpty()) {
			for (JSONObject order : orders) {
				WeixiuCart cart = new WeixiuCart();
				//添加订单
				cart.setOrderform(order);
				//添加订单详情
				cart.setOrderdetail(weixiuService.findGoods(order.getInteger("o_id")));
				listCart.add(cart);
			}
		}
		
		return listCart;
	}
}
